package com.sparrow.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Stack;

import com.sparrow.constant.*;

public class DateTimeUtil {
	/**
	 * 
	 * 获取时间间隔
	 * 
	 * @author zhanglizhi
	 * @date 2012-1-12
	 * @version 1.0
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param MILLIS_TIME_UNIT
	 *            以毫秒为时间单位
	 * @return
	 */
	public static int getInterval(Timestamp startTime, Timestamp endTime,
			int MILLIS_TIME_UNIT) {
		if (startTime == null || endTime == null) {
			return Integer.MIN_VALUE;
		}
		Calendar cal =Calendar.getInstance();
		cal.setTime(startTime);
		long start = cal.getTimeInMillis();
		cal.setTime(endTime);
		long end = cal.getTimeInMillis();
		return (int) (end - start) / MILLIS_TIME_UNIT;
	}

	/**
	 * 为当前时间加上指定单位的长度
	 * 
	 * @author zhanglizhi
	 * @date 2012-12-29
	 * @version 1.0
	 * @param calendar
	 *            Calendar.Date
	 * @param amount
	 *            时间长度
	 * @return
	 */
	public static Timestamp addTime(int calendar, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(System.currentTimeMillis()));
		cal.add(calendar, amount);
		return new Timestamp(cal.getTimeInMillis());
	}

	/**
	 * 获取系统当前时间并根据format进行格式化
	 * 
	 * @author zhanglizhi
	 * @date May 30, 2011
	 * @version 1.0
	 * @param format
	 * @return
	 */
	public static String getFormatCurrentTime(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(System.currentTimeMillis()));
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @author zhanglizhi
	 * @date May 30, 2011
	 * @version 1.0
	 * @return
	 */
	public static String getFormatCurrentTime() {
		return getFormatCurrentTime(COMMON.NORMAL_TIME_FORMAT);
	}

	public static String getFormatCurrentTimeWithms() {
		return getFormatCurrentTime("yyyy-MM-dd HH:mm:ss ms");
	}

	/**
	 * 获取格式化时间
	 * 
	 * @author zhanglizhi
	 * @date 2011-12-25
	 * @version 1.0
	 * @param timestamp
	 * @param format
	 * @return
	 */
	public static String getFormatTime(Timestamp timestamp, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(timestamp);
	}

	/**
	 * 获取**前的格式的时间
	 * 
	 * @author zhanglizhi
	 * @date 2012-1-2
	 * @version 1.0
	 * @return
	 */
	public static String getBeforeFormatTime(Timestamp timestamp) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(timestamp);
		long timesplit = (System.currentTimeMillis() - cal.getTimeInMillis()) / 1000;
		Iterator<String> keyit = MILLIS_TIME_UNIT.BEFOR_FORMAT.keySet()
				.iterator();
		Stack<String> result = new Stack<String>();
		String beforeFormat = new String();
		do {
			String key = keyit.next();
			int value = Integer.valueOf(MILLIS_TIME_UNIT.BEFOR_FORMAT.get(key));
			result.push(timesplit % value + key);
			timesplit = (timesplit / value);
		} while (timesplit > 0);
		if (!result.isEmpty()) {
			beforeFormat = result.pop();
		}
		return beforeFormat.toString();
	}
}
